<script lang="ts" setup>
import {onMounted, ref} from 'vue'
import {ElMessage, ElTable} from "element-plus";
import {userPageService,userEditService,userDeleteService} from '@/api/user/user'

interface User {
  id: number;
  username: string; //条码
  phone: string;
  password:string;
}

// 列表与表单
const form = ref<User>({})
const tableData = ref(<User[]> [])
// 抽屉表单是否显示
const updateDrawer = ref(false)
// 商品分类列表查询
const getUser = async ()=>{
  tableData.value = await userPageService()
}

// 挂载函数，渲染列表
onMounted(async ()=>{
  await getUser()
})

// 删除
const del=(row)=>{
  let deleteIds = row.id
  let params = {
    ids: deleteIds
  }
  console.log(params.ids)
  userDeleteService(params).then(()=>{
    ElMessage.success("删除成功")
    getUser()
  })
}

// 修改数据回显
const updateEcho=(row)=>{
  updateDrawer.value = true
  form.value={...row}
}

// 修改
const update =()=>{
  let params = {...form.value}
  userEditService(params).then(()=>{
    ElMessage.success("修改成功")
    updateDrawer.value = false
    getUser()
    form.value={}
  })
}

// 取消修改
const updateCancel =()=>{
  updateDrawer.value = false
  form.value = {}
}

// 表单校验规则
const rules = ref({
  username: [
    { required: true, message: '请输入用户名称', trigger: 'blur' },
    { min: 1, max: 20, message: '名称长度在1-20个字符之间', trigger: 'blur' }
  ],
  phone: [
    { required: true, message: '请输入手机号码', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur' }
  ],
  password: [
    {
      pattern: /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d!@#$%^&*]{8,20}$/,
      message: '密码格式不正确，必须是8-20位，含字母和数字',
      trigger: 'blur'
    }
  ],
})
</script>

<template>
  <!-- 表格 -->
  <div class="table-wrapper" style="width: 100%">
    <el-table height="80vh" ref="multipleTableRef" :data="tableData">
      <el-table-column property="username" min-width="100" label="用户名称"/>
      <el-table-column property="phone" min-width="100" label="手机号码"/>
      <el-table-column label="操作" min-width="100">
        <template #default="row">
          <el-button link type="primary"  @click="updateEcho(row.row)" >编辑</el-button>
          <el-popconfirm title="确认删除?" @confirm='del(row.row)'>
            <template #reference>
              <el-button link type="primary">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
  </div>

  <!-- 修改抽屉 -->
  <el-drawer v-model="updateDrawer" title="编辑用户" :show-close="false" >
    <el-form :model="form" :rules="rules" style="width: 80%" label-width="auto">
      <el-form-item label="用户名称:" prop="username">
        <el-input v-model="form.username" />
      </el-form-item>
      <el-form-item label="设置新密码:" prop="password">
        <el-input v-model="form.password" />
      </el-form-item>
      <el-button type="primary" @click="update">修改</el-button>
      <el-button @click="updateCancel">取消</el-button>
    </el-form>
  </el-drawer>

</template>

<style scoped>
/* 表格盒子 */
.table-wrapper{
  margin-left: 2vh;
  margin-top: 2vh;
  //border-right: black solid 1px;
}

/* 按钮组件 */
.button-group {
  margin-left: 3vh;
  margin-top: 5px;
}

/* 按钮组件-按钮 */
.button-group .el-button {
  width: 15vh;
}
</style>